﻿/**
** 初始化测试数据
*/
var geodata = new Array();
var bhdlayer = null;

for(var i = 0; i< 10;i++)
{
	
	var lon = Math.round(Math.random()*50 + 80);
	var lat = Math.round(Math.random()*40 + 10);
	
	var lonlat = lon +',' + lat;
	geodata.push(lonlat);
}	
/**
 **@method 页面加载
*/
//设置气泡layer
var overlay = null;
$(function(){
	//初始化地图
	mapUtil.initializeMap();
		
	//添加矢量图层
	/*var geojsonObject = createFeaturesJson(geodata);
	var jsonlayer = mapUtil.addGeojsonLayer("样点数据图", geojsonObject);*/
	

	var url = "static/data/National.geojson";
	//var url = 'http://127.0.0.1:8080/examples/test_map/data/countries.geojson';
	
	bhdlayer = mapUtil.addGeojsonLayerByUrl("国家级保护区", url, bhdStyleCall);

    ///添加地图选择事件
    /// createOverlayContent 回调函数，生成overlay 内容
    mapUtil.createSelectSingleClick(createOverlayContent);
    //获取选中集
    var select = mapUtil.selectFeatue;
    //单击事件
    mapUtil.singleClick(bhdlayer, select);

	///追加线类测试数据
	/**************************************************/
	//var count = jsonlayer.getSource().getFeatures().length;

	//var coordinates = [[117,40],[117.5,40],[117,40.5]];

	//addFeature(jsonlayer, count+1, coordinates, "Line", "123");

	/********************************************************/
	
	///添加添加节点数据图层（temp) 可以不用后期再修改
	//var nodelayer = mapUtil.addNodesLayer();
	
	
	///添加popu for pointermove ---(popup123固定id)
	var popup = olGis.addPopOverlay("popup123");
	
	/// 注册鼠标在地图移动事件
	registerPointerMoveEvent('保护区名称');
	
	
	///添加地图工具地图点击事件
	////"drawPoint", "drawLine",
	var tools = ["zoomOut", "zoomIn","dragIn","save","drawSurface","measureLength","measureArea","clearMeasure","clearSecelted","download","reset","viewIn"];

	olGis.addMapToolbar(tools);
	
	
	/// 添加图层控制列表
	olGis.showLayerTree();
	
	
	///设置图层可编辑(当前加载矢量图 bhdlayer)
	drawGeom.setEditLayer(bhdlayer);
});

///页面特殊功能
/**
 * 气泡中点击详情弹出框
 * @param id
 */
function showNatureDetail(fId,coordinateStr){
	top.coordinateStr = coordinateStr;
    //首页详情弹框新增修改标识
    top.flag = 1;
	jo.postAjax("travel/reserve/get.action",{"id" : fId},function(json){
		if(json.data[0] && json.code == 0){
			//jo.showMsg("查询出一条记录");
            //top.fId = fId;
            top.jo.showWin("page/nature/natureForm.jsp?id="+fId, '90%', '90%', '保护区详情', 'WIN_CHECK');
		}else{
            top.fId = fId;
            top.jo.showWin("page/nature/natureForm.jsp", '90%', '90%', '保护区详情', 'WIN_CHECK');
		}
	},true);
}
/**
 *@method 点击保存按钮，弹出信息保存界面
 *@return 
*/
function showInfoModalForm(sUrl,sWidth,sHeight,sTitle){
	sUrl = "page/nature/natureForm.jsp?"
    if(jo.isValid(sUrl)){
        sUrl = sUrl + jo.getLinkSign(sUrl) + joView.params["transferParam"];
        top.jo.showWin(sUrl,jo.getDefVal(sWidth, joView.params["formWidth"]),jo.getDefVal(sHeight, joView.params["formHeight"]),"新建自然保护区资源单体信息", joView.params["winNameOfAdd"]);
    }
};
/**
 *@method 气泡里显示信息内容
 *@param feature 回调函数，选中要素（提取信息）
 *@return 
*/
function createOverlayContent(feature) {
	//var id = feature.get('id');
	//获取保护区id
	var fId = feature.getId();
	//对保护区id进行解析
	fId = fId.split(".")[1];
	//对应关联表格行
	var hdms = ol.coordinate.toStringHDMS(feature.getGeometry().getCoordinates());

	//获取坐标字符串
	var coordinateStr = feature.getGeometry().getCoordinates();
    coordinateStr = JSON.stringify(coordinateStr);
    coordinateStr = coordinateStr.substring(1);
    coordinateStr = coordinateStr.substring(0,coordinateStr.length-1);
	var popcontent = ' <h2>Norwegian Mountain Trip</h2><img border="0" src="static/images/pulpit.jpg" alt="Pulpit rock" width="304" height="228"><a onclick="showNatureDetail(\''+fId+'\',\''+coordinateStr+'\');">保护地详情!</a><code>' + hdms + '</code>';

	return popcontent;
}
/**
 *@method 样式回调函数
 *@param feature 要素信息
 *@return
 */
function bhdStyleCall(featrue, resolution){

	var type = featrue.get('功能分区');
	var col = null;
	if(type == "保护区" || type == "baohuqu"){
		col = 'rgba(127, 255, 127, 1)';

	}else if (type == "缓冲区") {
		col = 'rgba(223, 191, 159, 1)';
	}
	else if (type == "实验区") {
		col = 'rgba(255, 255, 127, 1)';
	}
	else if (type == "核心区") {
		col = 'rgba(127, 191, 127, 1)';
	}
	else{
		col = 'rgba(127, 255, 127, 0.5)';
	}
	var style = new ol.style.Style({
//		  stroke: new ol.style.Stroke({
//							  color: 'blue',
//							  lineDash: [4],
//							  width: 3
//			}),
		fill: new ol.style.Fill({
			color: col
		})

	});

	return style;
}

//搜索定位展示
function searchPosition(checkObj){
    if(checkObj){
        //console.info(bhdlayer);
        //获取当前选中对象的feature
        var source = bhdlayer.getSource();
        if(source instanceof ol.source.Cluster){
            source = source.getSource();
        }
        var feature = source.getFeatureById("National."+checkObj.id);
        console.info("feature:"+feature);
        if(feature){

        	//获取选择集
        	var select = mapUtil.selectFeatue;
        	console.info("select:"+select);
            select.getFeatures().clear();
            //将要素添加到选择集中
            select.getFeatures().push(feature);

              var extent = feature.getGeometry().getExtent();
              //获取横坐标最小值
              var minx = extent[0];
              //获取横坐标最大值
			  var maxx = extent[2];
			  //获取矩形的长度
			  var length =  (maxx - minx)/2;
			  minx = minx - length;
			  maxx = maxx + length;
              extent[0] = minx;
              extent[2] = maxx;
            //视野拉框放大
            //var mapExtent = ol.extent.createEmpty();
            map.getView().fit(extent,map.getSize());
        }

        //map.getView().setCenter([formatXY(checkObj.longitude), formatXY(checkObj.latitude)]);
    }else{
        jo.showMsg("未选中待定位保护地!", {icon:2});
    }


}